package com.zjinyin.forum.service.impl;

import com.zjinyin.forum.common.AppResult;
import com.zjinyin.forum.common.ResultCode;
import com.zjinyin.forum.dao.ArticleReplyMapper;
import com.zjinyin.forum.exception.ApplicationException;
import com.zjinyin.forum.model.Article;
import com.zjinyin.forum.model.ArticleReply;
import com.zjinyin.forum.service.IArticleReplyService;
import com.zjinyin.forum.service.IArticleService;
import com.zjinyin.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-08-11
 * Time: 18:28
 */

/**
 * 关于回复帖子中功能接口的具体实现
 */

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource
    private IArticleService articleService;


    /**
     * 创建用户评论帖子即回复帖子, 同时更新该帖子下的回复数
     *
     * @param articleReply 回复帖子
     */
    @Override
    public void create(ArticleReply articleReply) {
        // 1. 参数校验 - 那个帖子的回复 articleId  回复的是谁的帖子 postUserId  回复内容 content
        if (articleReply == null || articleReply.getArticleId() == null
                || articleReply.getPostUserId() == null || StringUtil.isEmpty(articleReply.getContent())) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 2. 设置默认值
        // 回复关联编号, 楼中楼
        articleReply.setReplyId(null);
        // 楼中楼的楼主编号
        articleReply.setReplyUserId(null);
        // 点赞数
        articleReply.setLikeCount(0);
        // 回复帖子状态
        articleReply.setState((byte) (0));
        // 创建和更新时间
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);

        // 3. 写入数据库
        int row = articleReplyMapper.insertSelective(articleReply);

        // 4. 检验结果
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES + " , 受影响行数不为 1 错误");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES) + " , 受影响行数不为 1 错误");
        }

        // 更新文章帖子表中的回复数
        articleService.addOneReplyCountById(articleReply.getArticleId());

        log.info("回复帖子创建成功 : ArticleId : " + articleReply.getArticleId() + " postUserId : " + articleReply.getPostUserId());
    }


    /**
     * 根据主贴 id 查看帖子下的所有回复帖子
     *
     * @param articleId 主贴 id
     */
    @Override
    public List<ArticleReply> selectReplyArticleById(Long articleId) {
        // 1. 参数校验
        if (articleId == null || articleId <= 0) {
            log.warn("主贴 ArticleId : " + articleId + " " + ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 2. 根据articleId 查询是否存在该帖子
        Article article = articleService.selectById(articleId);

        if (article == null) {
            log.warn("主贴 ArticleId : " + articleId + " " + ResultCode.FAILED_NOT_EXISTS);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }

        if (article.getState() == 1) {
            log.warn("主贴 ArticleId : " + articleId + " " + ResultCode.FAILED_ARTICLE_ABNORMAL);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_ABNORMAL));
        }

        if (article.getDeleteState() == 1) {
            log.warn("主贴 ArticleId : " + articleId + " " + ResultCode.FAILED_ARTICLE_NOT_EXISTS);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }

        // 3. 查询帖子列表
        List<ArticleReply> list = articleReplyMapper.selectReplyArticleById(articleId);

        if (list == null) {
            return new ArrayList<>();
        }

        log.info("ArticleId : " + articleId + " 成功查询所有回复帖子");
        return list;
    }
}
